package com.sy.controller;


import com.sy.dto.StudentClassTreeResult;
import com.sy.entity.Student;
import com.sy.entity.Teacher;
import com.sy.service.ClassService;
import com.sy.service.GradeService;
import com.sy.service.StudentService;
import com.sy.service.TeacherService;

import com.sy.vo.StudentPagingVo;
import com.sy.vo.TeacherPagingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


import static com.sy.constant.BusinessConstant.NULL;

/**
 * @Author:ZHF
 * @Date: 2021/10/12 14:09
 */
@RestController
public class UpdateByAdminController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassService classService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 查询操作
     * @param
     * @return 所有学生数量
     */
    @RequestMapping(value = "studentsNum", method = RequestMethod.GET)
    public Integer studentsNum() {
        Integer studentsSize = studentService.getStudentsSize();
        return studentsSize;
    }

    /**
     * 查询操作
     * @param
     * @return 学生分页后的信息
     */
    @RequestMapping(value = "students", method = RequestMethod.GET, params = {"pageSize", "pageNum", "grade",
            "className", "studentName"})
    public List<Student> students(StudentPagingVo studentPagingVo) {
        System.out.println(studentPagingVo);
        System.out.println("null".equals(studentPagingVo.getGrade()));
        if ((NULL.equals(studentPagingVo.getGrade()) || NULL.equals(studentPagingVo.getClassName())) &&
                NULL.equals(studentPagingVo.getStudentName())) {
            List<Student> students = studentService.queryAllStudents(studentPagingVo.getPageNum(), studentPagingVo.getPageSize());
            return students;
        }
        if (!NULL.equals(studentPagingVo.getGrade()) && !NULL.equals(studentPagingVo.getClassName())
                && NULL.equals(studentPagingVo.getStudentName())) {
            List<Student> students = studentService.queryStudentsByGradeAndClassName(studentPagingVo);
            return students;
        }
        if (!NULL.equals(studentPagingVo.getStudentName()) &&
                (NULL.equals(studentPagingVo.getGrade()) || NULL.equals(studentPagingVo.getClassName()))) {

            List<Student> students = studentService.queryStudentsByStudentName(studentPagingVo);
            return students;
        }
        return null;

    }

    /**
     * 查询操作
     * @param
     * @return 班级和年级信息
     */
    @RequestMapping(value = "gradesAndClassName", method = RequestMethod.GET)
    public List<StudentClassTreeResult> getGradesAndClassName() {
        List<StudentClassTreeResult> gradeAndClassName = studentService.getGradeAndClassName();
        return gradeAndClassName;
    }

    /**
     * 查询操作
     * 通过年级和班级名字获取学生数量
     * 通过年级和班级名字获取学生数量进行分页操作
     * @param grade,className
     * @return 学生数量
     */
    @RequestMapping(value = "students", method = RequestMethod.GET, params = {"grade", "className"})
    public Integer studentsNumByGradeAndClassName(String grade, String className) {
        return studentService.getStudentsNumByGradeAndClassName(grade, className);
    }

    /**
     * 查询操作
     * 通过学生姓名获取学生数量
     * 通过学生姓名获取学生数量进行分页操作
     * @param studentName
     * @return 学生数量
     */
    @RequestMapping(value = "students", method = RequestMethod.GET, params = {"studentName"})
    public Integer studentsNumByStudentName(String studentName) {
        return studentService.getStudentsNumByStudentName(studentName);
    }

    /**
     * 获取所有班级名字
     * @return 班级名字
     */
    @RequestMapping(value = "classNames", method = RequestMethod.GET)
    public List<String> getAllClassName() {
        return classService.getAllClassName();
    }

    /**
     * 获取所有年级名字
     * @return 年级名字
     */
    @RequestMapping(value = "grades", method = RequestMethod.GET)
    public List<String> getAllGrade() {
        return gradeService.getAllGrade();
    }

    /**
     * 获取编辑后的学生信息对数据进行修改
     *
     * @param student
     * @return 是否成功
     */
    @RequestMapping(value = "redactStudent", method = RequestMethod.POST)
    public Boolean redactStudent(@RequestBody Student student) {
        System.out.println("+++++"+student);
        Boolean aBoolean = studentService.redactStudent(student);
        return aBoolean;
    }

    /**
     * 获取新增后的学生信息对学生数据进行新增
     *
     * @param student
     * @return 是否成功
     */
    @RequestMapping(value = "addStudent", method = RequestMethod.POST)
    public Boolean addStudent(@RequestBody Student student) {
        Boolean aBoolean = studentService.addStudents(student);
        return aBoolean;
    }

    /**
     * 对学生信息删除操作
     *
     * @param id
     * @return 是否成功
     */
    @RequestMapping(value = "delStudent", method = RequestMethod.POST)
    public Boolean delStudent(Integer id) {
        return studentService.delStudentById(id);
    }

    /**
     * 获取老师数量进行分页操作
     *
     * @return 老师数量
     */
    @RequestMapping(value = "teachers", method = RequestMethod.GET)
    public Integer getTeachersNum() {
        return teacherService.getTeachersNum();
    }

    /**
     * 查询操作

     * 通过教师姓名或工号获取教师数量进行分页操作
     * @param teacherNameOrTno
     * @return 教师数量
     */
    @RequestMapping(value = "teachers", method = RequestMethod.GET, params = {"teacherNameOrTno"})
    public Integer teachersNumByTeacherNameOrTno(String teacherNameOrTno) {
        return teacherService.getTeacherNumByTeacherNameOrTno(teacherNameOrTno);
    }

    /**
     * 查询操做
     * 获取教师数量进行分页操作
     *
     * @return 老师信息
     */
    @RequestMapping(value = "teachers", method = RequestMethod.GET, params = {"pageSize", "pageNum", "teacherNameOrTno"})
    public List<Teacher> teachers(TeacherPagingVo teacherPagingVo) {

        if (NULL.equals(teacherPagingVo.getTeacherNameOrTno())) {
            List<Teacher> allStudents = teacherService.getAllTeachers(teacherPagingVo);
            return allStudents;
        } else {
            List<Teacher> allTeachersByTeacherNameOrTno = teacherService.getAllTeachersByTeacherNameOrTno(teacherPagingVo);
            return allTeachersByTeacherNameOrTno;
        }

    }

    /**
     * 更改操做
     * 对教师状态更改为不可读
     *
     * @return 成功与否
     */
    @RequestMapping(value = "delTeacher", method = RequestMethod.POST)
    public Boolean delTeacher(Integer id) {

        return teacherService.delTeacherById(id);
    }

    /**
     * 更改操做
     * 对教师信息进行修改
     * @return 成功与否
     */
    @RequestMapping(value = "redactTeacher", method = RequestMethod.POST)
    public Boolean redactTeacher(@RequestBody Teacher teacher) {
        return teacherService.redactTeacher(teacher);
    }

    /**
     * 添加操做
     * 新增教师信息
     * @return 成功与否
     */
    @RequestMapping(value = "addTeacher", method = RequestMethod.POST)
    public Boolean addTeacher(@RequestBody Teacher teacher) {
        return teacherService.addTeacher(teacher);
    }
}
